#include "includes.h"
#include "structs.h"
#include "declares.h"
void my_adjlist_destory(my_adjlist_t *al)
{
        int vn=al->vn;
        
        for(int i=0;i<vn;++i) {
                igraph_vector_ptr_free_all(&(al->al[i]));
        }
        free(al->al);
}

int my_adjlist_init_empty(my_adjlist_t *al, int vn)
{
        zlog_category_t *mylog = zlog_get_category("mylog");
        int rc;

        if(vn <= 0) {
                zlog_error(mylog, "vn <= 0");
                return -1;
        }
        if(NULL == al) {
                zlog_error(mylog, "al is NULL");
                return -1;
        }
        al->al = xmalloc(sizeof(igraph_vector_ptr_t) * vn);
        for (int i=0;i<vn;++i) {
                rc = igraph_vector_ptr_init(&(al->al[i]), 0);
                igraph_vector_ptr_set_item_destructor(&(al->al[i]), free);
        }
        al->vn = vn;
        al->en = 0;
        return 0;
}

/* get the adjlist for vertex i
 * Input: al -> a bundle of adjlists 
 *      i -> vertex i
 * Ouput: adjlist of i
 * Error: NULL 
 */
igraph_vector_ptr_t *my_adjlist_get(my_adjlist_t *al, int i)
{
        zlog_category_t *mylog = zlog_get_category("mylog");
        if(i<0 || i>=al->vn) {
                zlog_error(mylog, "invalid i %d", i);
                return NULL;
        }
        return &(al->al[i]);
}

/* print textual info of a my_adjlist_t */
void my_adjlist_print(FILE *s, my_adjlist_t *al)
{
        int vn = al ->vn;
        fprintf(s, "my_adjlist_t(vn=%d)\n", al->vn);
        for(int i=0;i<vn;++i) {
                igraph_vector_ptr_t *pa = &(al->al[i]);
                int size = igraph_vector_ptr_size(pa);
                fprintf(s, "\tvid=%d\n", i+1);
                for(int j=0;j<size;++j) {
                        struct edge_t *pe = VECTOR(*pa)[j];
                        /* fprintf(s,"%d ", pe->dst+1); */
                        fprintf(s, "\t\t");
                        print_edge(s, pe);
                }
                fprintf(s, "\n");
        }
}

